package me.w70.bot.scripting;

import java.awt.Graphics;
import java.io.File;
import java.util.HashMap;

import javax.imageio.ImageIO;

import me.w70.bot.Bot;
import me.w70.bot.methods.Methods;

public abstract class Script extends Methods implements Runnable {
	public boolean running = false;

	public abstract String getScriptName();

	public boolean usesGUI = false;

	public boolean doneGUI = false;

	public abstract String getScriptAuthor();

	public abstract String getScriptCategory();

	public abstract String getScriptDescription();

	private long startTime = 0000;

	private long pauseTime = 0000;

	private boolean checkRandoms = true;

	protected boolean hasRun = false;

	public GUIBuilder gui;

	public abstract int loop() throws Exception;

	public abstract void draw(Graphics g);

	public final boolean isRunning() {
		return running;
	}

	public void afterGUI(HashMap args) {

	}

	public final void checkRandoms() {
		if (checkRandoms) {
			Bot.randomChecker.check();
		}
	}
	
	protected final void ignoreRandoms() {
		checkRandoms = false;
	}

	protected boolean onLoad() {
		return true;
	}

	protected void onEnd() {

	}

	protected void onPause() {

	}

	protected void onResume() {

	}

	boolean createdDir = false;

	public void takeScreenshot(String name) {

		String ext = ".png";
		String classn = getClass().getSimpleName();
		String[] p = classn.split("\\.");
		classn = p[p.length - 1] + "/";
		if (!createdDir) {
			try {
				createdDir = (new File(Bot.SCRIPT_IMG_DIR + "/" + classn))
						.mkdir();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		File s = new File(Bot.SCRIPT_IMG_DIR + classn + name + ext);
		if (s.exists()) {
			for (int i = 0; i < 100; i++) {
				s = new File(Bot.SCRIPT_IMG_DIR + classn + name + "-" + i + ext);
				if (!s.exists()) {
					break;
				}
			}
		}

		if (s.exists()) {
			Bot.addMessage(this, "Tried to take screenshot -- failed");
			return;
		}

		if (Bot.currentScreen == null) {
			return;
		}

		try {
			ImageIO.write(Bot.currentScreen, "png", s);
		} catch (Exception e) {

		}

	}

	protected final void startTimer() {
		if (startTime == 0000) {
			startTime = System.currentTimeMillis();
		}
	}

	public final boolean hasRun() {
		return hasRun;
	}

	public final String getFormattedRunningTime() {
		long elapsed = System.currentTimeMillis() - startTime - pauseTime;
		long time = elapsed / 1000;
		String seconds = Integer.toString((int) (time % 60));
		String minutes = Integer.toString((int) ((time % 3600) / 60));
		String hours = Integer.toString((int) (time / 3600));
		for (int i = 0; i < 2; i++) {
			if (seconds.length() < 2) {
				// seconds = "0" + seconds;
			}
			if (minutes.length() < 2) {
				// minutes = "0" + minutes;
			}
		}
		return hours + "h " + minutes + "m " + seconds + "s";
	}

	long startPause = 0000;

	public final void pause(boolean on) {

		this.running = on;
		if (!on) {
			onPause();
			startPause = System.currentTimeMillis();
		} else {
			onResume();
			pauseTime = pauseTime + (System.currentTimeMillis() - startPause);
		}
	}

	public void run() {
		hasRun = true;
		while (true) {
			if (running) {
				startTimer();
				try {
					if (!Bot.isRandomRunning()) {
						int w = loop() * 1;
						if (w < 0) {
							running = false;
							return;
						}
						wait(w);
						if (!getScriptCategory().contains("Debug")) {
							checkRandoms();
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					wait(300);
					if (!getScriptCategory().contains("Debug")) {
						checkRandoms();
					}
				}
			}
		}
	}

	public final boolean isUnknown() {
		try {
			boolean a = getScriptName() == null;
			boolean b = getScriptName().equals("");
			if (a || b) {
				return true;
			}
			return false;
		} catch (Exception e) {
			return false;
		}
	}

}
